#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/>
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host

DST=$1
VM_ID=$2
DS_ID=$3

#--------------------------------------------------------------------------------

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
    LIB_LOCATION=/usr/lib/one
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
    LIB_LOCATION=$ONE_LOCATION/lib
fi

DRIVER_PATH=$(dirname $0)

source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
source ${DRIVER_PATH}/../../datastore/libfs.sh

CEPH_UTILS=${DRIVER_PATH}/../../datastore/ceph/ceph_utils.sh

#-------------------------------------------------------------------------------
# Process destination
#-------------------------------------------------------------------------------

DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

#-------------------------------------------------------------------------------
# Delete and exit if directory
#-------------------------------------------------------------------------------

if [ `is_disk $DST_PATH` -eq 0 ]; then
    # Directory: delete checkpoint and directory

    unset i j XPATH_ELEMENTS

    while IFS= read -r -d '' element; do
        XPATH_ELEMENTS[i++]="$element"
    done < <(onedatastore show -x $DS_ID | $XPATH \
                        /DATASTORE/TEMPLATE/SOURCE \
                        /DATASTORE/TEMPLATE/CLONE \
                        /DATASTORE/TEMPLATE/CEPH_USER \
                        /DATASTORE/TEMPLATE/CEPH_CONF \
                        /DATASTORE/TEMPLATE/POOL_NAME)

    SRC="${XPATH_ELEMENTS[j++]}"
    CLONE="${XPATH_ELEMENTS[j++]}"
    CEPH_USER="${XPATH_ELEMENTS[j++]}"
    CEPH_CONF="${XPATH_ELEMENTS[j++]}"
    POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"

    if [ -n "$CEPH_USER" ]; then
        RBD="$RBD --id ${CEPH_USER}"
    fi

    if [ -n "$CEPH_CONF" ]; then
        RBD="$RBD --conf ${CEPH_CONF}"
    fi

    RBD_SRC_CHECKPOINT="${POOL_NAME}/one-sys-${VM_ID}-checkpoint"

    ssh_exec_and_log "$DST_HOST" "$RBD rm $RBD_SRC_CHECKPOINT 2>/dev/null || exit 0" \
                 "Error deleting $RBD_SRC_CHECKPOINT in $DST_HOST"

    log "Deleting $DST_PATH"
    ssh_exec_and_log "$DST_HOST" "rm -rf $DST_PATH" "Error deleting $DST_PATH"
    exit 0
fi

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(echo "$DST_PATH" | $AWK -F. '{print $NF}')

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID | $XPATH \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_CONF \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/POOL_NAME)

SRC="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"
CEPH_CONF="${XPATH_ELEMENTS[j++]}"
POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"

# No need to delete no cloned images
if [ "$CLONE" = "NO" ]; then
    exit 0
fi

if [ -n "$CEPH_USER" ]; then
    RBD="$RBD --id ${CEPH_USER}"
fi

if [ -n "$CEPH_CONF" ]; then
    RBD="$RBD --conf ${CEPH_CONF}"
fi

if [ -n "$SRC" ]; then
    # cloned, so the name will be "<pool>/one-<imageid>-<vmid>-<diskid>"
    RBD_SRC="${SRC}-${VM_ID}-${DISK_ID}"
    RBD_SNAP="${VM_ID}-${DISK_ID}"
else
    # volatile
    RBD_SRC="${POOL_NAME}/one-sys-${VM_ID}-${DISK_ID}"
    RBD_SNAP=""
fi

#-------------------------------------------------------------------------------
# Delete the device
#-------------------------------------------------------------------------------

log "Deleting $DST_PATH"

DELETE_CMD=$(cat <<EOF
    RBD="${RBD}"

    if [ "\$(rbd_format $RBD_SRC)" = "2" ]; then
        rbd_rm_r \$(rbd_top_parent $RBD_SRC)

        if [ -n "$RBD_SNAP" ]; then
            rbd_rm_snap $SRC $RBD_SNAP
        fi
    else
        $RBD rm $RBD_SRC
    fi
EOF
)

ssh_exec_and_log_stdin "$DST_HOST" "$DELETE_CMD" "$CEPH_UTILS" \
                 "Error deleting $RBD_SRC in $DST_HOST"
